import api from './api'
import {
  appid,
  appsecret,
  requstPromise,
  getCode
} from '../utils/utils'

const request = {
  async getQuestions(option) {
    return requstPromise(await api.get('/getQuestions', option))
  },
  async dailyQuestions(option) {
    return requstPromise(await api.get('/dailyQuestions', option))
  },
  async verifyAnswer(option) {
    return requstPromise(await api.post('/verifyAnswer', option))
  },
  async startQuestions(option) {
    return requstPromise(await api.post('/startQuestions', option))
  },
  async getWxConfig(option) {
    return requstPromise(await api.get('/wxConfig', option))
  },
  async share(option) {
    return requstPromise(await api.post('/share', option))
  },
  async addUserFriend(option) {
    return requstPromise(await api.post('/addUserFriend', option))
  },
  async getAllList(option) {
    return requstPromise(await api.get('/getAllList', option))
  },
  async getFriendList(option) {
    return requstPromise(await api.get('/getFriendList', option))
  },
  async projectInfo(option) {
    return requstPromise(await api.get('/projectInfo', option))
  },
  async getGoods(option) {
    return requstPromise(await api.get('/getGoods', option))
  },
  async getGoodsInfo(option) {
    return requstPromise(await api.get('/getGoodsInfo', option))
  },
  async getLuckyDrawRecord(option) {
    return requstPromise(await api.get('/getLuckyDrawRecord', option))
  },
  async getUserInfo(option) {
    let res = await api.post('/getUserInfo', option)
    console.log(res)
    if (!res.data) {
      getCode()
      return
    }
    return requstPromise(res)
  },
  async exchangeGoods(option) {
    let res = await api.post('/exchangeGoods', option)
    return requstPromise(res)
  },
  async getLuckyDraw(option) {
    let res = await api.get('/getLuckyDraw', option)
    return requstPromise(res)
  },
  async luckyDraw(option) {
    let res = await api.post('/luckyDraw', option)
    return requstPromise(res)
  },
  async getWXAccessToken(option) {
    let res = await api.post('/wxinterface', option)
    console.log(res)
    if (res && !res.success) {
      // debugger
      getCode()
      return
    }
    localStorage.setItem('user', JSON.stringify(res.data))
    return requstPromise(res)
    // const access_token = await api.get(`https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${appsecret}&code=${code}&grant_type=authorization_code`)
    // request.getWXuserInfo(access_token)
  },
  async getWXuserInfo(access_token) {
    // console.log(access_token)
    // const wxUser = await api.get(`https://api.weixin.qq.com/sns/userinfo?access_token=${access_token.access_token}&openid=${access_token.openid}&lang=zh_CN`)
    // if (wxUser.errcode) {
    //   getCode()
    //   return
    // }
    // const login = await api.post('/login', wxUser)
    // localStorage.setItem('user', JSON.stringify(login.data))
    // return requstPromise(login)
  },
}

export default request
